import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class BookMangement {
    Book s;
    Scanner scanner = new Scanner(System.in);
    ArrayList<Book> book = new ArrayList<Book>();

    public BookMangement() {
        book.add(new Book(1, "西游记", "吴承恩", "龙岩出版社", 48));
        book.add(new Book(2, "三国演义", "罗贯中", "上海出版社", 58));
        book.add(new Book(3, "水浒传", "施耐庵", "北京出版社", 48));
        book.add(new Book(4, "红楼梦", "曹雪芹", "深圳出版社", 50));
    }

    public void add() {
        boolean s = true;
        System.out.println("请输入图书编号");
        int isbn = scanner.nextInt();
        System.out.println("请输入书名");
        String name = scanner.next();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        System.out.println("请输入出版社");
        String press = scanner.next();
        System.out.println("请输入作者");
        String author = scanner.next();
        for (int i = 0; i < book.size(); i++) {
            if (book.get(i).isbn == isbn) {
                System.out.println("编号已存在");
                s = false;
            }
            if (book.get(i).name == name) {
                System.out.println("书名已存在");
                s = false;
            }
            if (i == book.size() - 1 && s == true) {
                book.add(new Book(isbn, name, author, press, price));
                System.out.println("添加成功");
                break;
            }
        }
    }

    public void delect() {
        boolean s = false;
        System.out.println("请输入要删除的图书编号");
        int isbn = scanner.nextInt();
        for (int i = 0; i < book.size(); i++) {
            if (book.get(i).isbn == isbn) {
                book.remove(i);
                System.out.println("删除成功");
                s = true;
            }
            if (i == book.size() - 1 && s == false) {
                System.out.println("编号不存在");
                break;
            }
        }
    }

    public void update() {
        boolean s = false;
        System.out.println("请输入想要更新的图书编号");
        int isbn = scanner.nextInt();
        for (int i = 0; i < book.size(); i++) {
            if (book.get(i).isbn == isbn) {
                System.out.println("请输入修改后的isbn");
                int newisbn = scanner.nextInt();
                System.out.println("请输入修改后的name");
                String newname = scanner.next();
                System.out.println("请输入修改后的press");
                String newpress = scanner.next();
                System.out.println("请输入修改后的price");
                int newprice = scanner.nextInt();
                System.out.println("请输入修改后的author");
                String newauthor = scanner.next();
                book.set(i, new Book(newisbn, newname, newauthor, newpress, newprice));
                System.out.println("更新成功");
                s = true;
            }
            if (i == book.size() - 1 && s == false) {
                System.out.println("您输入的isbn不存在");
                break;
            }
        }
    }

    public void select() {
        for (; ; ) {
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级");
            int s = scanner.nextInt();
            if (s == 1) {
                boolean w = false;
                System.out.println("请输入要查询的isbn");
                int isbn = scanner.nextInt();
                for (int i = 0; i < book.size(); i++) {
                    if (book.get(i).isbn == isbn) {
                        System.out.println(book.get(i));
                        w = true;
                    }
                    if (i == book.size() - 1 && w == false) {
                        System.out.println("编号不存在");
                        break;
                    }
                }
            }
            if (s == 2) {
                boolean w = false;
                System.out.println("请输入要查询的书名的关键词");
                String name = scanner.next();
                for (int i = 0; i < book.size(); i++) {
                    if (book.get(i).name.contains(name)) {
                        System.out.println(book.get(i));
                        w = true;
                    }
                    if (i == book.size() - 1 && w == false) {
                        System.out.println("该关键词不存在任何数据");
                        break;
                    }
                }
            }
            if (s == 3) {
                boolean w = false;
                System.out.println("请输入要查询的出版社");
                String press = scanner.next();
                for (int i = 0; i < book.size(); i++) {
                    if (book.get(i).press.equals(press)) {
                        System.out.println(book.get(i));
                        w = true;
                    }
                    if (i == book.size() - 1 && w == false) {
                        System.out.println("出版社不存在");
                        break;
                    }
                }
            }
            if (s == 4) {
                boolean w = false;
                System.out.println("请输入你要查询的作者");
                String author = scanner.next();
                for (int i = 0; i < book.size(); i++) {
                    if (book.get(i).author.equals(author)) {
                        System.out.println(book.get(i));
                        w = true;
                    }
                    if (i == book.size() - 1 && w == false) {
                        System.out.println("作者不存在");
                        break;
                    }
                }
            }
            if (s == 5) {
                boolean w = false;
                System.out.println("请输入你要查询最低价");
                int price = scanner.nextInt();
                System.out.println("请输入你要查询的最高价");
                int prices = scanner.nextInt();
                for (int i = 0; i < book.size(); i++) {
                    if (price <= book.get(i).price && book.get(i).price <= prices) {
                        System.out.println(book.get(i));
                        w = true;
                    }
                    if (i == book.size() - 1 && w == false) {
                        System.out.println("该价位不存在书籍");
                        break;
                    }
                }
            }
                if (s == 6) {
                    for (int i = 0; i < book.size(); i++) {
                        System.out.println(book.get(i));
                    }
                }
                if (s == 7) {
                    break;
                }
            }

        }
    }